package com.example.shop.service.impl;

import com.example.shop.domain.User;
import com.example.shop.mapper.UserMapper;
import com.example.shop.service.UserService;
import com.example.shop.utils.MD5Utils;
import com.example.shop.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @projectName: travel
 * @package: com.example.travel.service.impl
 * @className: UserService
 * @author: Liqian
 * @description: TODO
 * @date: 2024/4/11 9:07
 * @version: 1.0
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findUserByName(String username) {
        User u = userMapper.findUserByName(username);
        return u;
    }

    @Override
    public void register(String username, String password, String contact) {
        //MD5加密
        String md5password = MD5Utils.getMD5String(password);
        User tuser = new User();
        tuser.setUsername(username);
        tuser.setPassword(md5password);
        tuser.setContact(contact);
        userMapper.insert(tuser);
    }

    @Override
    public void update(User tuser) {
        User newTuser = new User();
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        newTuser.setId(id);
        newTuser.setNickname(tuser.getNickname());
        newTuser.setContact(tuser.getContact());
        userMapper.updateById(newTuser);
    }

    @Override
    public void updateAvatar(String string) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        User tuser = new User();
        tuser.setId(id);
        tuser.setUserPic(string);
        userMapper.updateById(tuser);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        User tuser = new User();
        tuser.setId(id);
        tuser.setPassword(MD5Utils.getMD5String(newPwd));
        userMapper.updateById(tuser);
    }

    @Override
    public List<User> getAllUser(User user) {
        return  userMapper.selectAllUser(user);
    }
}
